<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>主页</title>
	<link rel="stylesheet" href="css/bootstrap.min.css">
	<link rel="stylesheet" href="css/bootstrap-table.min.css">
	<script src="js/jquery-3.6.0.min.js"></script>
	<script src="js/bootstrap.min.js"></script>
	<script src="js/bootstrap-table.min.js"></script>
</head>
<body>
<div style="margin:50px 50px;width:auto;">
    <table id="table"></table>
</div>
	<script src="js/conf.js"></script>
    <script>
		let cookie=document.cookie
        $('#table').bootstrapTable({
            url: url+"/home",
			ajaxOptions:{headers: {'myCookie': cookie}},
            method: "post",
            contentType: "application/x-www-form-urlencoded",
            columns:[{
                field: 'id',
                title: '编号',
                align: "center",
				visible:false
            },{
                field: 'un',
                title: '账号',
                align: "center"
            },{
                field: 'name',
                title: '课程名',
                align: "center"
            },{
                field: 'url',
                title: '来自网站',
                align: "center"
            },{
                field: 'startDate',
                title: '开始时间',
                align: "center"
            },{
                field: 'endDate',
                title: '结束时间',
                align: "center"
            },{
                field: 'pwd',
                title: '登录密码',
                align: "center"
            },{
                field: 'progress',
                title: '当前进度',
                align: "center",
				formatter:function(value, row, index){return value+'%';}
            },{
				title:'操作',
				align: "center",
				events: {
				'click #add':function (e, value, row, index){if(row.progress!='100'){$.ajax({url: url+'/add',headers:{'mycookie':cookie},type: 'POST',data: { 'url':row.url,'id':row.id,'un':row.un,'pwd':row.pwd},success: function (response) {alert(response);if(response=='跳转中'){location.assign(row.url)}},error: function (xhr, status, error) {alert(error);}})}else{alert('课程进度100%')}},
				'click #edit':function (e, value, row, index){let pwd = prompt('请输入新密码：');if(pwd!='' && pwd!=null){$.ajax({url: url+'/updatepwd',headers:{'mycookie':cookie},type: 'POST',data: { 'un':row.un,'url':row.url,'pwd':pwd },success: function (response) {alert(response);$('#table').bootstrapTable('refresh');},error: function (xhr, status, error) {alert(error);}})}}},
				formatter:function (value, row, index){
				let res='<button id="add" class="btn btn-danger style="margin-left:10px;">开始计划</button>';
				res+='<button id="edit" class="btn btn-danger" style="margin-left:10px;">修改密码</button>';
				return res;}
			}]
        });
		$('#table').before('<div id="toolbar"><button id="del" class="btn btn-primary float-right" style="margin-left:10px;margin-top:10px;">删除账号</button></div>');
		$('#table').before('<div id="toolbar"><button id="addurl" class="btn btn-primary float-right" style="margin-left:10px;margin-top:10px;">添加网站</button></div>');
		$('#table').before('<div id="toolbar"><button id="update" class="btn btn-primary float-right" style="margin-left:10px;margin-top:10px;">更新所有</button></div>');
		$('#table').before('<div id="toolbar"><button id="import" class="btn btn-primary float-right" style="margin-left:10px;margin-top:10px;">一键导入</button></div>');
		$('#update').click(function(){$.ajax({url: url+'/update',headers:{'mycookie':cookie},type: 'POST',data: {},success: function (response) {alert(response);if(response=='请先登录'){location.assign('/login.html');};$('#table').bootstrapTable('refresh');},error: function (xhr, status, error) {alert(error);}})})
		$('#addurl').click(function(){let curl = prompt('请输入网址：');if(curl!='' && curl!=null){let un = prompt('请输入账号：');if(un!='' && un!=null){let pwd = prompt('请输入密码：');if(pwd!='' && pwd!=null){$.ajax({url: url+'/addurl',headers:{'mycookie':cookie},type: 'POST',data: { 'url': curl,'un':un,'pwd':pwd },success: function (response) {alert(response);if(response=='请先登录'){location.assign('/login.html');};$('#table').bootstrapTable('refresh');},error: function (xhr, status, error) {alert(error);}})}}}})
		$('#del').click(function(){let un = prompt('请输入账号：');if(un!='' && un!=null){$.ajax({url: url+'/del',headers:{'mycookie':cookie},type: 'POST',data: { 'un':un},success: function (response) {alert(response);if(response=='请先登录'){location.assign('/login.html');};$('#table').bootstrapTable('refresh');},error: function (xhr, status, error) {alert(error);}})}})
		$('#import').click(function(){let curl = prompt('请输入任一本校网课网址：');if(curl!='' && curl!=null){let un = prompt('请输入账号：');if(un!='' && un!=null){$.ajax({url: url+'/import',headers:{'mycookie':cookie},type: 'POST',data: {'url':curl,'un':un},success: function (response) {alert(response);if(response=='请先登录'){location.assign('/login.html');};$('#table').bootstrapTable('refresh');},error: function (xhr, status, error) {alert(error);}})}}})
		</script>
</body>
</html>